package com.usian.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmUserDto;
import com.usian.model.media.pojos.WmUser;
import com.usian.utils.common.AppJwtUtil;
import com.usian.wemedia.mapper.WmUserMapper;
import com.usian.wemedia.service.WmUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author baimugudu
 * @email 2415621370@qq.com
 * @date 2023/8/24 15:17
 */

@Service
@Transactional
@Slf4j
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper,WmUser> implements WmUserService
{
    @Override

    public ResponseResult saveWmUser(WmUser wmUser) {
        this.save(wmUser);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public WmUser findWmUserByName(String name) {
        QueryWrapper<WmUser> wmUserQueryWrapper = new QueryWrapper<>();
        wmUserQueryWrapper.lambda().eq(WmUser::getName,name);

        WmUser wmUser = this.baseMapper.selectOne(wmUserQueryWrapper);

        return wmUser;
    }

    @Override
    public ResponseResult login(WmUserDto wmUserDto) {
        if(null==wmUserDto){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        QueryWrapper<WmUser> wmUserQueryWrapper = new QueryWrapper<>();
        wmUserQueryWrapper.lambda().eq(WmUser::getName,wmUserDto.getName());
        WmUser wmUser = this.baseMapper.selectOne(wmUserQueryWrapper);
        if(null==wmUser){
            return ResponseResult.errorResult(AppHttpCodeEnum.WM_ADMIN_USER_NOT_EXIST);
        }else{
            String passwordDB = wmUser.getPassword();
            log.info("passwordDB:{}",passwordDB);
            String passwordUser = wmUserDto.getPassword();
            log.info("passwordUser:{}",passwordUser);
            //String passwordNew = DigestUtils.md5DigestAsHex((password + salt).getBytes());
            String newPassword = DigestUtils.md5DigestAsHex((passwordUser + wmUser.getSalt()).getBytes());
            log.info("newPassword:{}",newPassword);
            if(passwordDB.equals(newPassword)){
                Map hashMap = new HashMap();
                wmUser.setSalt("");
                wmUser.setPassword("");
                hashMap.put("wmUser",wmUser);
                Integer userId = wmUser.getId();
                String token = AppJwtUtil.getToken(userId.longValue());
                hashMap.put("token",token);

                return ResponseResult.okResult(hashMap);
            }else{
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }


        }


    }
}
